package com.yaodian100.ec.member.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.csvreader.CsvWriter;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.member.entity.BlackBook;
import com.yaodian100.ec.member.entity.BlackBookImportResult;
import com.yaodian100.ec.member.service.MemberService;

public class BlackBookAction extends ActionSupport implements Preparable {
	private static final String RESULT_CONFIRM_UNBLOCK_DIALOG = "confirmUnblockDialog";
	private static final String RESULT_CONFIRM_BLOCK_DIALOG = "confirmBlockDialog";
	private static final String RESULT_DOWNLOAD_LIST_FILE_EXAMPLE = "downloadListFileExample";
	private static final String RESULT_TO_IMPORT_BLACK_BOOK_LIST = "importBlackBookList";
	private static final String RESULT_IMPORT_BLACK_BOOK_LIST = "importBlackBookList";
	
	private static final long serialVersionUID = 1L;
	private static final String RESULT_LIST = "list";

	@Autowired
	private MemberService memberService;
	@Autowired
	private DomainService<Menu> menuService;
	
	private Menu memberBlockStatus;
	private Menu blockReasonMenu;
	private Menu unblockReasonMenu;
	
	private String addBlackBookByMemberIdResultMessage;
	private File uploadFile;
	private BlackBookImportResult importResult;
	private InputStream inputStream;
	
	private BlackBook blackBook;
	
	public String addBlackBookByMemberId() {
		BlackBook newBlackBook = this.memberService.addBlackBookByMemberId(this.blackBook);
		if (newBlackBook == null) {
			setAddBlackBookByMemberIdResultMessage("新增失败");
		} else {
			setAddBlackBookByMemberIdResultMessage("新增成功 ");
		}
		return RESULT_LIST;
	}

	public String unblockBlackBookByBlackBookId() {
		BlackBook updateResult = this.memberService.unblockBlackBook(this.blackBook);
		BeanUtils.copyProperties(updateResult, this.blackBook);
		
		if (updateResult == null) {
			setAddBlackBookByMemberIdResultMessage("解锁失败");
		} else {
			setAddBlackBookByMemberIdResultMessage("解锁成功 ");
		}
		
		return RESULT_LIST;
	}
	
	public String toImportBlackBookList() {
		return RESULT_TO_IMPORT_BLACK_BOOK_LIST;
	}
	
	public String downloadListFileExample() throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();

		CsvWriter writer = new CsvWriter(output, ',', Charset.forName("UTF-8"));
		writer.setUseTextQualifier(false);
		writer.writeRecord(new String[] { "000001XXXX", "1" ,"恶意订购记录至少50次" });
		writer.writeRecord(new String[] { "000002XXXX", "2" ,"" });
		writer.writeRecord(new String[] { "000003XXXX", "1" ,"被查到用机器人" });
		writer.flush();
		writer.close();
		inputStream = new ByteArrayInputStream(output.toByteArray());
		output.flush();
		output.close();

		return RESULT_DOWNLOAD_LIST_FILE_EXAMPLE;
	}

	public String importBlackBookList() throws FileNotFoundException, IOException {
		BlackBookImportResult importResult = this.memberService.importBlackBookList(uploadFile);
		this.importResult = importResult;
		
		return RESULT_IMPORT_BLACK_BOOK_LIST;
	}

	public String search() {
		return RESULT_LIST;
	}

	public String confirmBlockDialog() {
		return RESULT_CONFIRM_BLOCK_DIALOG;
	}

	public String confirmUnblockDialog() {
		return RESULT_CONFIRM_UNBLOCK_DIALOG;
	}

	public void prepare() throws Exception {
		if (null == memberBlockStatus) {
			memberBlockStatus = menuService.get("Member.BlockStatus");
		}
		if (null == blockReasonMenu) {
			blockReasonMenu = menuService.get("BlackBook.BlockReason");
		}
		if (null == unblockReasonMenu) {
			unblockReasonMenu = menuService.get("BlackBook.UnblockReason");
		}
	}

	public String getAddBlackBookByMemberIdResultMessage() {
		return this.addBlackBookByMemberIdResultMessage;
	}
	public void setAddBlackBookByMemberIdResultMessage(String addBlackBookByMemberIdResultMessage) {
		this.addBlackBookByMemberIdResultMessage = addBlackBookByMemberIdResultMessage;
	}

	public MemberService getMemberService() {
		return this.memberService;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public Menu getMemberBlockStatus() {
		return this.memberBlockStatus;
	}
	public void setMemberBlockStatus(Menu memberBlockStatus) {
		this.memberBlockStatus = memberBlockStatus;
	}

	public DomainService<Menu> getMenuService() {
		return this.menuService;
	}
	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public File getUploadFile() {
		return this.uploadFile;
	}
	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public BlackBookImportResult getImportResult() {
		return this.importResult;
	}

	public void setImportResult(BlackBookImportResult importResult) {
		this.importResult = importResult;
	}

	public InputStream getInputStream() {
		return this.inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public Menu getBlockReasonMenu() {
		return this.blockReasonMenu;
	}
	public void setBlockReasonMenu(Menu blockReasonMenu) {
		this.blockReasonMenu = blockReasonMenu;
	}

	public Menu getUnblockReasonMenu() {
		return this.unblockReasonMenu;
	}
	public void setUnblockReasonMenu(Menu unblockReasonMenu) {
		this.unblockReasonMenu = unblockReasonMenu;
	}

	public BlackBook getBlackBook() {
		return this.blackBook;
	}
	public void setBlackBook(BlackBook blackBook) {
		this.blackBook = blackBook;
	}

}